define([
  'jquery',
  'underscore',
  'backbone',
  'views/View',
  'bootstrap_modal',
  'bootstrap_transition'
], function($, _, Backbone, View){
  var ModalView = View.extend({
    tagName:		"div",
    className:		"modal hide fade",
    
    events: {
      "click button.close":             "close",
      "click button[name=ok]":             "close",
      "click button[name=save]":	"save",
      "click button[name=cancel]":	"cancel"

    },

    initialize: function(options) {
      options || (options = {});
      if(options.template)  {
          this.template = options.template;
      }

      if(options.flags) {
        this.flags = options.flags;
      }
      //console.log("ModalEditor -> initialize");
      
      //$(this.el).attr("id", this.model.isNew() ? "new" : "edit-" + this.model.get("id"));
      _.bindAll(this,
//                'successHandler',
//		'errorHandler',
//		'save',
//		'stopSubmit',
		'cancel',
		'close',
                'shown');
      
      if(this.model) {
        this.model.bind("success_save", this.successHandler, this);
        this.model.bind("error", this.errorHandler, this);
      }
      //console.log(this.model.save);
    },
    
    render: function() {
      View.prototype.render.call(this);
      this.renderContain();
      $(this.el).modal({"show": true, keyboard: true, "data-backdrop": true, "backdrop": true});
      $(this.el).on('hide', this.close);
      $(this.el).on('shown', this.shown);
      
      return this;
    },

    shown: function() {
      //this.trigger("shown");
    },
    
    renderContain: function() {
      $(this.el).html(this.template(this.getRenderData()));
    },
    
    save: function() {
      // This method should be implemented in subclasses
    },
    
    cancel: function() {
      //console.log("ModalForm -> cancel");
      this.close();
    },

    getRenderData: function() {
      var data = View.prototype.getRenderData.call(this);
      data || (data = {});
      data.flags = this.flags || {};

      return data;
    }

  });

  return ModalView;
});